package away3d.animators ;


import away3d.animators.data.SkeletonAnimationSequence ;
import away3d.animators.data.SkeletonAnimationState ;
import away3d.animators.skeleton.SkeletonTimelineClipNode ;
import away3d.animators.skeleton.SkeletonTreeNode ;



/**
 * AnimationSequenceController provides a controller for single clip-based animation sequences ( fe: md2, md5anim ).
 */
class SkeletonAnimator extends SkeletonAnimatorBase
{
    
    
    //TODO: check if this should be an Object !!
    private var _sequences  : Array<SkeletonAnimationSequence> ;
    private var _clipNode   : SkeletonTimelineClipNode ;
    
    
    
    /**
     * Creates a new AnimationSequenceController object.
     */
    public function new( target : SkeletonAnimationState )
    {
        
        super( target ) ;
        _sequences = [] ;
        
    }
    
    override protected function createBlendTree() : SkeletonTreeNode
    {
        
        return _clipNode = new SkeletonTimelineClipNode() ;
        
    }
    
    
    /**
     * Plays a sequence with a given name. If the sequence is not found, it may not be loaded yet, and it will retry every frame.
     * @param sequenceName The name of the clip to be played.
     */
    public function play( sequenceName : String )
    {
        
        _clipNode.clip = _sequences[ sequenceName ] ;
        
        if ( !_clipNode.clip )
        {
            
            throw new Error( "Clip not found!" ) ;
            
        }
        
        _clipNode.time = 0 ;
        
        start() ;
        
    }
    
    
    /**
     * Adds a sequence to the controller.
     */
    public function addSequence( sequence : SkeletonAnimationSequence )
    {
        
        _sequences[ sequence.name ] = sequence ;
        
    }
    
    
    /**
     * @inheritDoc
     * @private
     */
    override protected function updateAnimation( realDT : Float, scaledDT : Float )
    {
        
        _clipNode.time += scaledDT / _clipNode.clip.duration ;
        super.updateAnimation( realDT, scaledDT ) ;
        
    }
    
    
}